#pragma once

#include "doc/normal_document.h"
#include "index/attribute_index/attribute_indexer.h"
#include "util/status.h"
#include "util/pool/pool.h"

namespace hawking {
namespace indexlib {

class AttributeMemIndexer : public AttributeIndexer {
public:
    AttributeMemIndexer(
        int32_t field_id,
        util::ColumnType field_type,
        std::shared_ptr<AttributeConvertor> attribute_convertor) :
            AttributeIndexer(field_id, field_type, attribute_convertor) {}
    virtual ~AttributeMemIndexer() = default;

    virtual void Init() = 0;

    virtual Status AddDocument(NormalDocument* doc) {
        return AddField_(GetFieldData_(doc));
    }

    virtual Status BatchRead(SegmentContext* segment_ctx);

    virtual Status Dump(
        const std::string& attr_dir,
        const std::unordered_map<DocId, DocId>& old2new_map) = 0;

    Pool* GetPool() {
        return pool_.get();
    }

    size_t MemoryUse() const {
        return pool_->UsedBytes();
    }

protected:
    void BaseInit_() {
        allocator_ = std::make_unique<MMapAllocator>();
        pool_ = std::make_unique<Pool>(allocator_.get(), 10 * 1024 * 1024);
    }
    virtual Status AddField_(const std::string_view& value) = 0;
    virtual ReadOneDoc_(DocId doc_id, util::FieldValue* field_value) = 0;

    std::unique_ptr<MMapAllocator> allocator_;
    std::unique_ptr<Pool> pool_;
};

}
}